package cn.edu.sgu.www.mhxysy.service.chongwu_shizhuang.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.chongwu_shizhuang.ChongwuShizhuangViewAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_shizhuang.ChongwuShizhuangViewAttribute;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_shizhuang.ChongwuShizhuangViewAttributeMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.service.chongwu_shizhuang.ChongwuShizhuangViewAttributeService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuShizhuangViewAttributeServiceImpl implements ChongwuShizhuangViewAttributeService {

    private final ChongwuShizhuangViewAttributeMapper chongwuShizhuangViewAttributeMapper;

    @Autowired
    public ChongwuShizhuangViewAttributeServiceImpl(ChongwuShizhuangViewAttributeMapper chongwuShizhuangViewAttributeMapper) {
        this.chongwuShizhuangViewAttributeMapper = chongwuShizhuangViewAttributeMapper;
    }

    @Override
    public void insert(ChongwuShizhuangViewAttributeDTO chongwuShizhuangViewAttributeDTO) {
        ChongwuShizhuangViewAttribute chongwuShizhuangViewAttribute = toEntity(chongwuShizhuangViewAttributeDTO);

        chongwuShizhuangViewAttribute.setId(null);

        chongwuShizhuangViewAttributeMapper.insert(chongwuShizhuangViewAttribute);
    }

    @Override
    public void deleteById(Integer id) {
        chongwuShizhuangViewAttributeMapper.deleteById(id);
    }

    @Override
    public void updateById(ChongwuShizhuangViewAttributeDTO chongwuShizhuangViewAttributeDTO) {
        ObjectUtils.requireNonNull(chongwuShizhuangViewAttributeDTO.getId(), CustomMessages.ID_NOT_NULL);

        ChongwuShizhuangViewAttribute chongwuShizhuangViewAttribute = toEntity(chongwuShizhuangViewAttributeDTO);

        chongwuShizhuangViewAttributeMapper.updateById(chongwuShizhuangViewAttribute);
    }

    @Override
    public ChongwuShizhuangViewAttribute select(Integer grade, Integer propertyId) {
        QueryWrapper<ChongwuShizhuangViewAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("grade", grade);
        queryWrapper.eq("property_id", propertyId);

        return chongwuShizhuangViewAttributeMapper.selectOne(queryWrapper);
    }

    @Override
    public Page<ChongwuShizhuangViewAttribute> selectByPage(Pager<ChongwuShizhuangViewAttribute> pager) {
        Page<ChongwuShizhuangViewAttribute> page = Pager.ofPage(pager);

        return chongwuShizhuangViewAttributeMapper.selectPage(page, null);
    }

    private ChongwuShizhuangViewAttribute toEntity(ChongwuShizhuangViewAttributeDTO chongwuShizhuangViewAttributeDTO) {
        ChongwuShizhuangViewAttribute chongwuShizhuangViewAttribute = new ChongwuShizhuangViewAttribute();

        chongwuShizhuangViewAttribute.setId(chongwuShizhuangViewAttributeDTO.getId());
        chongwuShizhuangViewAttribute.setMax(chongwuShizhuangViewAttributeDTO.getMax());
        chongwuShizhuangViewAttribute.setMin(chongwuShizhuangViewAttributeDTO.getMin());
        chongwuShizhuangViewAttribute.setGrade(chongwuShizhuangViewAttributeDTO.getGrade());
        chongwuShizhuangViewAttribute.setPropertyId(chongwuShizhuangViewAttributeDTO.getPropertyId());

        return chongwuShizhuangViewAttribute;
    }

}